/**
 * Created by 51ibm on 2017/4/26.
 */
import Constant from '../constant/constant'
import {
    Dimensions,
    PixelRatio,
} from 'react-native'

import ToastUtil from './toast-util'
const numeral = require('numeral')

let appStore = null;

export default class Util {
    static setStore(store){
        appStore = store;
    }

    static getStore(){
        return appStore;
    }

    static getEmpId(){
        let empId=""
        if(appStore){
            const {user} = appStore.getState()
            if(user.hasOwnProperty('userInfo') && user.userInfo.hasOwnProperty('empId')){
                empId = parseInt(user.userInfo.empId);
            }
        }
        return empId
    }

    static getToken(){
        let token=""
        if(appStore){
            const {user} = appStore.getState()

            if(user.hasOwnProperty('userInfo') && user.userInfo.hasOwnProperty('token')){
                token = user.userInfo.token;
            }
        }
        return token
    }

    static uuid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
        }

        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    }

    static getTimeBySeconds(seconds) {
        let time = Math.round(seconds);
        let s = time % 60;
        let m = (time - s) / 60;
        if (s < 10) {
            s = '0' + s;
        }
        return m + ':' + s;
    }

    //imageindex@url的格式中，取出url
    static substrImageUrl(value) {
        let url = '';

        if (value) {
            url = value.substring(value.lastIndexOf("@") + 1)
            if (url.substring(0, 7) === '/media/') {
                url = Constant.serverUrl + url;
            }
        }
        return url;
    }

    //imageindex@url的格式中，取出imageIndex
    static substrImageIndex(value) {
        let url = value.substring(0, value.indexOf("@"));
        return url;
    }

    static showToast(msg, cfg) {
        ToastUtil.show(msg, cfg)
    }

    static showGlobalLoading(instance) {
        if (instance && typeof(instance.setState) === "function") {

            instance.setState({
                showGlobalLoading: true,
            });
        }

    }

    static hiddenGlobalLoading(instance) {
        if (instance && typeof(instance.setState) === "function") {
            instance.setState({
                showGlobalLoading: false,
            });
        }
    }

    static getScreenWidth() {
        return Dimensions.get('window').width;
    }

    static getScreenHeight() {
        return Dimensions.get('window').height;
    }

    static getRealPixel(needPixel) {
        return needPixel / PixelRatio.get()
    }

    static formatNumber(number, format) {
        return numeral(number).format(format)
    }

    //转换图片Url数组到照片展示专用Photo结构
    static convertImageUrlsToPhotos(imageUrls) {

        let photos = [];
        imageUrls.map((item, index) => {
            photos.push({
                photo: item,
            })
        })

        return photos;
    }

    //判断字段为空
    static isEmpty(value) {
        if (value == null || typeof(value) == 'undefined' || value == '') {
            return true;
        } else {
            return false;
        }
    }

    //数字格式化
    static formatNumber(num, precision='2', separator) {
        var parts;
        // 判断是否为数字
        if (!isNaN(parseFloat(num)) && isFinite(num)) {
            // 把类似 .5, 5. 之类的数据转化成0.5, 5, 为数据精度处理做准, 至于为什么
            // 不在判断中直接写 if (!isNaN(num = parseFloat(num)) && isFinite(num))
            // 是因为parseFloat有一个奇怪的精度问题, 比如 parseFloat(12312312.1234567119)
            // 的值变成了 12312312.123456713
            num = Number(num);
            // 处理小数点位数
            num = (typeof precision !== 'undefined' ? num.toFixed(precision) : num).toString();
            // 分离数字的小数部分和整数部分
            parts = num.split('.');
            // 整数部分加[separator]分隔, 借用一个著名的正则表达式
            parts[0] = parts[0].toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + (separator || ','));

            return parts.join('.');
        }
        return NaN;
    }

}




